<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Register Controller
 *
 * @package		SimplyPost
 * @subpackage	Member
 * @category	Controller
 * @author		Pascal Kriete
 */

class Register extends BaseController {

	/**
	 * Controller
	 *
	 * @access	public
	 */
	function Register()
	{
		parent::BaseController();
		$this->lang->load('member');
		$this->load->library('validation');
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Show and validation registration form
	 *
	 * @access	public
	 */
	function index()
	{
		set_title($this->lang->line('member_register_title'));
		$this->breadcrumb->from_path();
		
		$this->load->helper('form');

		$rules['usernamve'] = "trim|required|xss_clean";
		$rules['email'] = "trim|required|valid_email";
		$rules['password'] = "trim|required";
		$rules['submit'] = "callback__verify";
		$this->validation->set_rules($rules);
		
		$fields['username'] = ' username';
		$fields['email'] = 'n e-mail address';
		$fields['password'] = ' password';
		$fields['submit'] = 'Registration Data';
		$this->validation->set_fields($fields);
		
		$this->validation->set_message('required', 'You must enter a%s.');
		$this->validation->set_message('valid_email', 'The email address you entered is not valid.');
		$this->validation->set_error_delimiters('<p style="color:#900">', '</p>');
		
		if ($this->validation->run() == FALSE)
		{
			foreach($fields as $field => $name)
			{
				$data[$field] = $this->validation->$field;
				$err_field = $field.'_error';
				$data[$err_field] = $this->validation->$err_field;
			}
			
			$this->parser->parse('member/register', $data);
		}
		else
		{
			$this->load->model('User_model', 'User');
			$this->User->create($_POST['username'], $_POST['email'], $_POST['password']);
			
			$this->session->set_flashdata('msg', $this->lang->line('register_success'));
			redirect('member/login');
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Registration validation callback
	 *
	 * @access	private
	 * @param	string
	 * @return	bool
	 */
	function _verify($data)
	{
		$username = $this->input->post('username');
		$email = $this->input->post('email');
		$password = $this->input->post('password');
		$password = secure_password($email, $password);
		
		$ans = $this->plugin->process_register();
				
		if ($ans[0] === FALSE) {
			$this->validation->set_message('_verify', $ans[1]);
			return FALSE;
		}
		
		$this->load->model('User_model', 'User');
		if($this->User->exists(array('username' => $username)) )
		{
			$this->validation->set_message('_verify', $this->lang->line('username_taken'));
			return FALSE;
		}
		else if( $this->User->exists(array('email' => $email)) )
		{
			$this->validation->set_message('_verify', $this->lang->line('email_taken'));
			return FALSE;
		}
		else
		{
			$_POST['password'] = $password;
			return TRUE;
		}
	}
}